package cn.rdq.iknessplatform.controller;


import cn.rdq.api.bo.DramaBo;
import cn.rdq.api.dto.DramaCommentDto;
import cn.rdq.api.po.Drama;
import cn.rdq.api.po.Episode;
import cn.rdq.api.vo.DramaVo;
import cn.rdq.common.CommonResult;
import cn.rdq.common.constant.SysCurrentInfo;
import cn.rdq.iknessplatform.service.*;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;

@RestController
@RequestMapping("/drama")
@Api
public class DramaController {

    @Autowired
    private DramaService dramaService;

    @Autowired
    private DramaCommentService dramaCommentService;

    @Autowired
    private EpisodeService episodeService;

    @Autowired
    private UserPursueService userPursueService;

    @Autowired
    private BlogService blogService;

    @Autowired
    private CarouselService carouselService;

    @Autowired
    private Executor asyncServiceExecutor;


    @GetMapping("/{id}")
    public CommonResult detail(@PathVariable Long id) throws InterruptedException {
        DramaBo dramaBo = new DramaBo();
        Drama drama = dramaService.getById(id);
        dramaBo.setDrama(drama);
        CountDownLatch countDownLatch = new CountDownLatch(3);
        asyncServiceExecutor.execute(() -> {
            dramaBo.setComments(dramaCommentService.listRootComment(drama.getId()));
            countDownLatch.countDown();
        });
        asyncServiceExecutor.execute(() -> {
            dramaBo.setEpisodes(episodeService.listByDramaId(drama.getId()));
            countDownLatch.countDown();
        });
        asyncServiceExecutor.execute(() -> {
            dramaBo.setPopular(dramaService.mostViews());
            countDownLatch.countDown();
        });
        countDownLatch.await();
        return CommonResult.successObj(dramaBo);
    }

    @GetMapping("/index")
    public CommonResult indexPage() throws InterruptedException {
//        Map<String, List<Drama>> map = new HashMap<String, List<Drama>>();
        Map<String, Object> map = new HashMap<>();
        CountDownLatch countDownLatch = new CountDownLatch(7);
        asyncServiceExecutor.execute(() -> {
            map.put("dramasZh", dramaService.top9ByPublishTimeByArea("中国"));
            countDownLatch.countDown();
        });
        asyncServiceExecutor.execute(() -> {
            map.put("dramasEn", dramaService.top9ByPublishTimeByArea("美国"));
            countDownLatch.countDown();
        });
        asyncServiceExecutor.execute(() -> {
            map.put("dramasJp", dramaService.top9ByPublishTimeByArea("日本"));
            countDownLatch.countDown();
        });
        asyncServiceExecutor.execute(() -> {
            map.put("movies", dramaService.lastByCategory("1"));
            countDownLatch.countDown();
        });
        asyncServiceExecutor.execute(() -> {
            map.put("animates", dramaService.lastByCategory("3"));
            countDownLatch.countDown();
        });
        asyncServiceExecutor.execute(() -> {
            map.put("popular", dramaService.mostViews());
            countDownLatch.countDown();
        });

        asyncServiceExecutor.execute(() -> {
            map.put("carouselList", carouselService.list());
            countDownLatch.countDown();
        });

        countDownLatch.await();

//        map.put("dramasZh", dramaService.top9ByPublishTimeByArea("中国"));
//        map.put("dramasEn", dramaService.top9ByPublishTimeByArea("美国"));
//        map.put("dramasJp", dramaService.top9ByPublishTimeByArea("日本"));
//        map.put("movies", dramaService.lastByCategory("1"));
//        map.put("animates", dramaService.lastByCategory("3"));
//        map.put("popular", dramaService.mostViews());


        return CommonResult.successObj(map);
    }

    @PostMapping("/area")
    public CommonResult areaTop(@RequestBody DramaVo dramaVo) {
        List<Drama> tvPrograms = dramaService.top9ByPublishTimeByArea(dramaVo.getArea());
        return CommonResult.successObj(tvPrograms);
    }

    /**
     * 热门剧目
     *
     * @return
     */
    @GetMapping("/polular")
    public CommonResult popular() {
        List<Drama> tvPrograms = dramaService.mostViews();
        return CommonResult.successObj(tvPrograms);
    }

    @PostMapping("/query")
    public CommonResult query(@RequestBody DramaVo dramaVo) {
        Map<String, Object> map = new HashMap<>();
        List<Drama> dramaList = dramaService.query(dramaVo);
        return CommonResult.success().pageSuccess(dramaList);
    }


}
